package com.dkd.manage.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.domain.Node;
import com.dkd.manage.domain.VmType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.VendingMachineMapper;
import com.dkd.manage.domain.VendingMachine;
import com.dkd.manage.service.IVendingMachineService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备管理Service业务层处理
 * 
 * @author gyw
 * @date 2025-03-07
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService 
{
    @Autowired
    private VendingMachineMapper vendingMachineMapper;

    @Autowired
    private VmTypeServiceImpl vmTypeService;

    @Autowired
    private NodeServiceImpl nodeService;
    @Autowired
    private ChannelServiceImpl channelService;
    /**
     * 查询设备管理
     * 
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id)
    {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 查询设备管理列表
     * 
     * @param vendingMachine 设备管理
     * @return 设备管理
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine)
    {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertVendingMachine(VendingMachine vendingMachine)
    {
//        1-1.插入设备基本信息
        vendingMachine.setInnerCode(UUIDUtils.getUUID());
        VmType vmType = vmTypeService.selectVmTypeById(vendingMachine.getVmTypeId());

        vendingMachine.setCreateTime(DateUtils.getNowDate());
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());
//        1-2.补充点位信息
        Node node = nodeService.selectNodeById(vendingMachine.getNodeId());
        BeanUtils.copyProperties(node,vendingMachine,"id");
//        由于addr与address字段不同，无法自动拷贝属性
        vendingMachine.setAddr(node.getAddress());
//       1-3.设置更新时间与创建时间
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        vendingMachine.setCreateTime(DateUtils.getNowDate());
        int result = vendingMachineMapper.insertVendingMachine(vendingMachine);
//        2-1补充货道信息
        /**
         * 货道编号写法为 ：设备的行-列
         */
        List<Channel> channelList = new ArrayList<>();
        for (int i = 1; i <=vmType.getVmRow(); i++) {
            for(int j = 1; j <= vmType.getVmCol(); j++){
                Channel channel = new Channel();
                String channelCode = i + "-" + j;
                channel.setChannelCode(channelCode);
                BeanUtils.copyProperties(vendingMachine,channel,"id");
                channel.setVmId(vendingMachine.getId());
                channel.setMaxCapacity(vendingMachine.getChannelMaxCapacity());
                channelList.add(channel);
            }

        }
        /**
         * 批量插入售货机货道表
         */
        channelService.insertChannelList(channelList);
        return result ;
    }

    /**
     * 修改设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine)
    {
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
//        当点位信息更改时，关联的设备信息也需要更新，比如详细地址，保持一致性
        if (vendingMachine.getNodeId() != null) {
            Node node = nodeService.selectNodeById(vendingMachine.getNodeId());
            vendingMachine.setAddr(node.getAddress());
            BeanUtils.copyProperties(node,vendingMachine,"id","updateTime");
        }
        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }

    /**
     * 批量删除设备管理
     * 
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids)
    {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备管理信息
     * 
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id)
    {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }

    /**
     * 根据设备编号查询设备信息
     * @param innerCode
     * @return
     */
    public VendingMachine selectVendingMachineVoByInnerCode(String innerCode) {
        return vendingMachineMapper.selectVendingMachineByInnerCode(innerCode);
    }
}
